//
//  ForgetViewController.m
//  图片故事
//
//  Created by 熊坤 on 16/3/10.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "ForgetViewController.h"
#import "Header.h"
#import "MBProgressHUD+PKX.h"
#import <BmobSDK/Bmob.h>
#import <BmobSDK/BmobQuery.h>
#import "LoginViewController.h"
@interface ForgetViewController ()

@property (nonatomic, strong) UITextField *pwdTextField;
@property (nonatomic, strong) UITextField *sureTextField;
@property (nonatomic, strong) UIButton *finishBtn;
@property (nonatomic, strong) UILabel *hintLabel;

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self initNavigation];
    
}

- (void)initUI {
    [self.view setBackgroundColor:Bg_Color];
    
    [self.view addSubview:self.pwdTextField];
    
    [self.view addSubview:self.sureTextField];
    
    [self.view addSubview:self.finishBtn];
    
}

- (void)initNavigation {
    self.title = @"更改密码";
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
}



#pragma mark - 点击事件


- (void)action_backItmeClickEvent {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)action_ClickedEvent {
    
    NSString *pwd = self.pwdTextField.text;
    NSString *surePwd = self.sureTextField.text;
    NSString *str = @"^[0-9a-zA-Z]{6,16}";
    NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    BOOL isMatch = [pred1 evaluateWithObject:surePwd];
    
    if (pwd == nil || pwd.length == 0 || surePwd.length == 0 || surePwd == nil) {
        
        [MBProgressHUD showError:@"密码不能为空"];
    } else if (![pwd isEqualToString:surePwd]){
        
        [MBProgressHUD showError:@"两次密码输入不相同， 请重新输入"];
        self.pwdTextField.text = @"";
        self.sureTextField.text = @"";
        
    } else if (!isMatch) {
        [MBProgressHUD showError:@"密码格式不正确或者不在6~16位之间"];
    } else {
        
        
        BmobQuery *query = [[BmobQuery alloc] init];
        NSString *sql = [NSString stringWithFormat:@"select * from user where userName = '%@'",_phoneNum];
        
        [query queryInBackgroundWithBQL:sql block:^(BQLQueryResult *result, NSError *error) {
            
            if (error) {
                NSLog(@"%@",[error localizedDescription]);
                
            } else if (result.resultsAry.count == 0) {
                
                [MBProgressHUD showError:@"没有此号码存在"];
                
            }else {
                NSLog(@"%@",result.resultsAry);
                NSDictionary *dict = result.resultsAry[0];

                if ([[dict objectForKey:@"userPassword"] isEqualToString:surePwd]) {
                    [MBProgressHUD showError:@"新密码不能和旧密码一致"];
                    self.pwdTextField.text = @"";
                    self.sureTextField.text = @"";
                    
                    return ;
                }
                
                BmobObject *user = [BmobObject objectWithoutDatatWithClassName:@"user" objectId:[NSString stringWithFormat:@"%@",[dict objectForKey:@"objectId"]]];
                
                [user setObject:surePwd forKey:@"userPassword"];
                [user updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                    
                    if (isSuccessful) {
                        [MBProgressHUD showSuccess:@"修改密码成功"];
                        
                        NSArray *arrayVc = self.navigationController.viewControllers;
                        for (UIViewController *vc in arrayVc) {
                            if ([vc isKindOfClass:[LoginViewController class]]) {
                                [self.navigationController popToViewController:vc animated:YES];
                            }
                        }
                        
                    } else {
                        [MBProgressHUD showError:@"修改密码失败"];
                    }
                }];
            }
        }];
    }
}


#pragma mark -- 懒加载

-(UILabel *)hintLabel {
    if (!_hintLabel) {
        _hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(My_Width(5), My_Height(100), SCREEN_W, My_Height(40))];
        _hintLabel.text = @"请输入您的新密码,密码6~16位的字母和数字组成，不包含空格、特殊字符、标点符号等";
        _hintLabel.font = [UIFont systemFontOfSize:16];
        [_hintLabel setTextColor:[UIColor grayColor]];
    }
    return _hintLabel;
}

- (UITextField *)pwdTextField {
    if (!_pwdTextField) {
        _pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(My_Width(0), My_Height(200), SCREEN_W, My_Height(50))];
        _pwdTextField.font = [UIFont systemFontOfSize:16];
        
        _pwdTextField.backgroundColor = Btn_Color;
        
        _pwdTextField.placeholder = @"请输入密码";
        
        [_pwdTextField setTextColor:[UIColor grayColor]];
        
        [_pwdTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
        
        _pwdTextField.textAlignment = NSTextAlignmentCenter;
        
        _pwdTextField.secureTextEntry = YES;
        
    }
    return _pwdTextField;
}

- (UITextField *)sureTextField {
    
    if (!_sureTextField) {
        
        _sureTextField = [[UITextField alloc] initWithFrame:CGRectMake(My_Width(0), My_Height(251), SCREEN_W, My_Height(50))];
        
        _sureTextField.font = [UIFont systemFontOfSize:16];
        
        _sureTextField.backgroundColor = Btn_Color;
        
        UIColor *color = [UIColor grayColor];
        
        _sureTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请确认密码" attributes:@{NSForegroundColorAttributeName: color}];
        
        [_sureTextField setTextColor:[UIColor grayColor]];
        
        _sureTextField.textAlignment = NSTextAlignmentCenter;
        
        _sureTextField.secureTextEntry = YES;
    }
    return _sureTextField;
}

- (UIButton *)finishBtn {
    if (!_finishBtn) {
        _finishBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _finishBtn.frame = CGRectMake(My_Width(20), My_Height(331), SCREEN_W - My_Width(50), My_Height(50));
        _finishBtn.backgroundColor = [UIColor colorWithRed:102 / 255.0 green:214 / 255.0 blue:228 / 255.0 alpha:1];
        _finishBtn.titleLabel.font = [UIFont systemFontOfSize:16];

        [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
        
        [_finishBtn.layer setCornerRadius:My_Height(5)];
        
        [_finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_finishBtn addTarget:self action:@selector(action_ClickedEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _finishBtn;
}

@end
